home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / Sample Code / CALib & You… / Source / CASample / EventLog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-07  |  2.3 KB  |  156 lines  |  [TEXT/MPS ]

  1.  
  2. #include "EventLog.h"
  3. #include <PLStringFuncs.h>
  4. #include <Files.h>
  5. #include <Events.h>
  6. #include <string.h>
  7.  
  8.  
  9. short    logRefNum;
  10.  
  11. static Boolean FormatOSEvent (EventRecord* event, char* str);
  12.  
  13.  
  14. void EventLog_StartLog(FSSpec* spec, Boolean flushLog)
  15. {
  16. FSSpec    logSpec;
  17. FInfo    ignore;
  18.  
  19.     if (spec == NULL)
  20.     {
  21.         PLstrcpy (logSpec.name, "\pCASample Event Log");
  22.         logSpec.vRefNum = 0;
  23.         logSpec.parID = 2;
  24.     }
  25.     else
  26.     {
  27.         logSpec = *spec;
  28.     }
  29.     
  30.     if (FSpGetFInfo (&logSpec, &ignore))
  31.     {
  32.         FSpCreate (&logSpec, 'ttxt', 'TEXT', 0);
  33.     }
  34.     
  35.     FSpOpenDF (&logSpec, fsRdWrPerm, &logRefNum);
  36.     
  37.     if (flushLog)
  38.         SetEOF (logRefNum, 0);
  39.         
  40. }
  41.  
  42.  
  43.  
  44. void EventLog_StopLog()
  45. {
  46.     FSClose (logRefNum);
  47. }
  48.  
  49. void EventLog_PrintComment (char* comment)
  50. {
  51. long    count;
  52. char    eol[2];
  53.  
  54.     count = strlen (comment) + 1;
  55.  
  56.     FSWrite (logRefNum, &count, comment);
  57.     
  58.     count = 1;
  59.     eol[0] = 0x0D;
  60.     FSWrite (logRefNum, &count, eol);
  61.  
  62. }
  63.  
  64. void EventLog_PrintEvent (EventRecord* event)
  65. {
  66. char        str[128];
  67. Str32        messg;
  68. char        eol[2];
  69. long        count;
  70. Boolean        printIt = true;
  71.  
  72.     NumToString  (event->message, messg);
  73.  
  74.     switch (event->what)
  75.     {
  76.     
  77.         case diskEvt:
  78.             strcpy(str, "diskEvt\t\t"); break;
  79.             
  80.         case activateEvt:
  81.             strcpy(str, "activateEvt\t"); break;
  82.             
  83.         case kHighLevelEvent:
  84.             strcpy(str, "kHighLevelEvent\t"); break;
  85.  
  86.         case mouseDown:
  87.             strcpy(str, "mouseDown\t\t"); break;
  88.             
  89.         case mouseUp:
  90.             strcpy(str, "mouseUp\t\t"); break;
  91.  
  92.         case updateEvt:
  93.             strcpy(str, "updateEvt\t\t"); break;
  94.  
  95.         case keyDown:
  96.             strcpy(str, "keyDown\t\t"); break;
  97.  
  98.         case nullEvent:
  99.             strcpy(str, "nullEvent\t\t"); break;
  100.  
  101.         case osEvt:
  102.             printIt = FormatOSEvent (event, str);    break;
  103.         
  104.         case autoKey:
  105.             strcpy(str, "autoKey\t\t"); break;
  106.  
  107.         case keyUp:
  108.             strcpy(str, "keyUp\t\t"); break;
  109.             
  110.         default:
  111.             strcpy(str, "UNKNOWN event"); break;
  112.     }
  113.     
  114.     
  115.     //strncat (str, (char*) &messg[1], (short) messg[0]);
  116.     //strcat (str, "");
  117.     
  118.     if (printIt)
  119.     {
  120.     
  121.         count = strlen (str) + 1;
  122.         FSWrite (logRefNum, &count, str);
  123.         count = 1;
  124.         eol[0] = 0x0D;
  125.         FSWrite (logRefNum, &count, eol);
  126.  
  127.     }
  128.     
  129. }
  130.  
  131.  
  132. static Boolean FormatOSEvent (EventRecord* event, char* str)
  133. {
  134. Boolean isSuspendEvent;
  135.  
  136.     switch ((event->message >> 24) & 0x0FF)
  137.     {
  138.         case mouseMovedMessage:
  139.             return false;
  140.  
  141.         case suspendResumeMessage:
  142.         
  143.             if (isSuspendEvent)
  144.                 strcpy(str, "osEvt\t\tSuspend");
  145.             else
  146.                 strcpy(str, "osEvt\t\tResumt");
  147.             return true;
  148.  
  149.         default:
  150.             break;
  151.             
  152.     }
  153.  
  154.     return false;
  155.  
  156. }